﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class Query : IEnumerable<SqlDataReader>, IDisposable
{
    public Query(string QueryString, Dictionary<SqlParameter, object> Parameters)
    {
        this.QueryString = QueryString;
        this.Parameters = Parameters;


        ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["mainConn"];

        if (ConnectionStringSettings == null || ConnectionStringSettings.ConnectionString.Trim() == "")
        {
            throw new Exception("Connection string cannot be blank.");
        }

        var connectionString = ConnectionStringSettings.ConnectionString;


        conn = new SqlConnection(connectionString);
        conn.Open();
    }

    string QueryString;
    Dictionary<SqlParameter, object> Parameters;
    SqlConnection conn;

    #region IEnumerable<SqlDataReader> Members

    public System.Collections.Generic.IEnumerator<SqlDataReader> GetEnumerator()
    {
        return new QueryEnumerator(QueryString, Parameters, conn);
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    #endregion

    public class QueryEnumerator : IEnumerator<SqlDataReader>
    {
        public QueryEnumerator(string QueryString, Dictionary<SqlParameter, object> Parameters, SqlConnection conn)
        {
            this.QueryString = QueryString;
            this.Parameters = Parameters;
            this.conn = conn;
            Reset();
        }
        string QueryString;
        Dictionary<SqlParameter, object> Parameters;
        SqlDataReader Reader;
        SqlConnection conn;

        #region IEnumerator<SqlDataReader> Members

        public SqlDataReader Current
        {
            get { return Reader; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {

        }

        #endregion

        #region IEnumerator Members

        object System.Collections.IEnumerator.Current
        {
            get { return Current; }
        }

        public bool MoveNext()
        {
            return Reader.Read();
        }

        public void Reset()
        {
            SqlCommand cmd = new SqlCommand(QueryString, conn);

            foreach (var item in Parameters)
            {
                cmd.Parameters.Add(item.Key).Value = item.Value;
            }


            Reader = cmd.ExecuteReader();
        }

        #endregion
    }

    #region IDisposable Members

    public void Dispose()
    {
        conn.Close();
    }

    #endregion
}
public static class DBCastExtensions
{
	public static T Nullable<T>(this object o)
	{
		if (o == DBNull.Value)
		{
			return default(T);
		}
		else
		{
			return (T)o;
		}
	}
    public static object ToDBNullable(this object o)
    {
        return (o == null) ? DBNull.Value : o;
    }
    //public static object ToDBNullable(this string o)
    //{
    //    return (o == "") ? DBNull.Value : (object)o;
    //}
}